package com.shihan.dao;

import com.shihan.io.Resource;
import com.shihan.pojo.User;
import com.shihan.sqlSession.MySqlSessionFactoryBuilder;
import com.shihan.sqlSession.SQLSession;
import com.shihan.sqlSession.SQLSessionFactory;

import java.io.InputStream;
import java.util.List;

/**
 * 原生的 DaoImpl 的方式，操作数据库；
 * （目前已经被 getMapper 代理的方式所代替）
 */
public class UserDaoImpl {

    public User findByCondition(User user) throws Exception {

        InputStream inputStream = Resource.getResourceAsStream("sqlMapConfig.xml");
        SQLSessionFactory sqlSessionFactory = new MySqlSessionFactoryBuilder().build(inputStream);
        SQLSession sqlSession = sqlSessionFactory.openSession();

        User user2 = sqlSession.selectOne("User.selectOne", user);
        System.out.println("Test selectOne：");
        System.out.println(user2);

        return user2;
    }

    public List<User> findAll() throws Exception {

        InputStream inputStream = Resource.getResourceAsStream("sqlMapConfig.xml");
        SQLSessionFactory sqlSessionFactory = new MySqlSessionFactoryBuilder().build(inputStream);
        SQLSession sqlSession = sqlSessionFactory.openSession();

        System.out.println("Test selectList:");
        List<User> users = sqlSession.selectList("User.selectList");
        for(User user1:users){
            System.out.println("User:" + user1);
        }

        return users;
    }

}
